package test;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibTest1 implements MethodInterceptor {
    @Override
    public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy proxy) throws Throwable {
        System.out.println("METHOD -> " + method.getName());
        return proxy.invokeSuper(obj, args);
    }

    public <T> T getInstance(final Class<T> t) {
        final Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(t);
        enhancer.setCallback(this);
        return (T) enhancer.create();
    }


    public static void main(String[] args) {
        CGLibTest1 cgLibTest1 = new CGLibTest1();
        CGLibTest1_A a = cgLibTest1.getInstance(CGLibTest1_AImpl.class);
//        a.fun();
//        a.fun2();
        a.fun3();
    }

}

interface CGLibTest1_A {
    void fun();
    void fun2();
    void fun3();
}

class CGLibTest1_AImpl implements CGLibTest1_A {
    public void fun() {
        System.out.println("A.fun");
    }

    public void fun2() {
        System.out.println("A.fun2");
    }

    public void fun3() {
        System.out.println("A.fun3");
        fun();
    }
}
